﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace DTC.Validations
{
    class FormValidate
    {
        public FormValidate()
        {
        }

        public bool FormHasError(List<string> errorList, DependencyObject obj)
        {
            GetErrors(errorList, obj);
            return errorList.Count() != 0;
        }

        public void GetErrors(List<string> errorList, DependencyObject obj)
        {
            foreach (object child in LogicalTreeHelper.GetChildren(obj))
            {
                if (child == null || child is string) continue;
                
                if (Validation.GetHasError((DependencyObject)child))
                {
                    if ((Validation.GetValidationAdornerSiteFor((DependencyObject)child)) != null)
                        errorList.Add((Validation.GetValidationAdornerSiteFor((DependencyObject)child)).GetValue(TextBlock.TextProperty) + " has errors:\r\n");
                    foreach (ValidationError error in Validation.GetErrors((DependencyObject)child))
                    {
                        errorList.Add(" " + error.ErrorContent.ToString() + "\r\n");
                    }
                    GetErrors(errorList, (DependencyObject)child);
                }
                GetErrors(errorList, (DependencyObject)child);
            }
        }
    }
}
